package com.dfrz.stockMarket.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dfrz.stockMarket.bean.MessageBean;
import com.dfrz.stockMarket.model.StocksModel;
import com.dfrz.stockMarket.model.UserOrderModel;

public class StockDao {
	public static void main(String[] args) {
		try {
			System.out.println(getStockByID(1).getMsg());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static MessageBean getAllStock() throws Exception {		
		
		List<StocksModel> list = new ArrayList<>();
		
		//加载驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		System.out.println("加载成功");
		//获取连接
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "hr", "hr");
		System.out.println("连接成功");
		//�?��事务
		conn.setAutoCommit(false);
		
		//获取stmt
		PreparedStatement stmt = conn.prepareStatement(" select stockcode, stockname, stockprice from sto_stocks");
		
		ResultSet rs = stmt.executeQuery();
		while(rs.next()){
			StocksModel stocksModel =new StocksModel();
			int stockcode = rs.getInt("stockcode");
			int stockprice = rs.getInt("stockprice");
			String stockname =rs.getString("stockname");
			
			stocksModel.setStockCode(stockcode);
			stocksModel.setStockName(stockname);
			stocksModel.setStockPrice(stockprice);
			
			list.add(stocksModel);
		}
		//提交or回滚
	
//		conn.rollback();
		//释放stmt conn
		rs.close();
		stmt.close();
		conn.close();
		System.out.println("stockcode:"+list.toString());
		return new MessageBean(true, list);
	}
	
	
	public static MessageBean getStockByID(int ID) throws Exception {	
		
		//加载驱动
				Class.forName("oracle.jdbc.driver.OracleDriver");
				System.out.println("加载成功");
				//获取连接
				Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "hr", "hr");
				System.out.println("连接成功");
				//�?��事务
				conn.setAutoCommit(false);
				
				//获取stmt
				PreparedStatement stmt = conn.prepareStatement("select stockcode, stockname, stockprice from sto_stocks where stockcode=?");
				stmt.setInt(1, ID);
				ResultSet rs = stmt.executeQuery();
				StocksModel stocksModel1=null;
				while(rs.next()){
					 stocksModel1 =new StocksModel();
					int stockcode = rs.getInt("stockcode");
					int stockprice = rs.getInt("stockprice");
					String stockname =rs.getString("stockname");
					System.out.println("stockname---"+stockname);
					stocksModel1.setStockCode(stockcode);
					stocksModel1.setStockName(stockname);
					stocksModel1.setStockPrice(stockprice);
					System.out.println("stocksModel1|stockname----"+stocksModel1.getStockName());
				}
				//提交or回滚
//				conn.rollback();
				//释放stmt conn
				rs.close();
				stmt.close();
				conn.close();
		return new MessageBean(true, stocksModel1);
	}
	
	public static MessageBean getSizeByID(int ID) {
		int size = 10;
		return new MessageBean(true, size);
	}
	
	public static MessageBean getAllSize() {
		int size = 10;
		return new MessageBean(true, size);
	}
}
